package com.jianglin.controller;

import com.github.pagehelper.PageInfo;
import com.jianglin.model.*;
import com.jianglin.service.IOwnerService;
import com.jianglin.service.IRepairService;
import com.jianglin.service.IRepairtypeService;
import com.jianglin.util.JsonObject;
import com.jianglin.util.JwtUtil;
import com.jianglin.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;
/**
 *
 *
 * @author liyijia
 */
@CrossOrigin(origins="http://localhost:5500")
@Api(tags = {"报修管理"})
@SuppressWarnings("SpellCheckingInspection")
@RestController
@RequestMapping("/repair")
public class RepairController {


    @Resource
    private IRepairService repairService;
    @Resource
    private IRepairtypeService repairtypeService;

    @Resource
    private IOwnerService ownerService;
    @Resource
    private JwtUtil jwtUtil;


    private boolean isAdminUser(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        if (authHeader != null && authHeader.startsWith("Bearer ")) {
            String token = authHeader.substring(7).trim();
            String username = jwtUtil.getUsernameFromToken(token);
            // 在实际项目中，这里应该检查用户角色
            // 简化示例：假设用户名为 "admin" 的是管理员
            return "admin".equals(username);
        }
        return false;
    }

    @RequestMapping("/queryRepairAll")
    public JsonObject<Repair> queryRepairAll(Repair repair,
                                     @RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "15") Integer limit,
                                     HttpServletRequest request) { // 添加request参数
        boolean isAdmin = isAdminUser(request);
        PageInfo<Repair> pageInfo = repairService.findRepairAll(page, limit, repair, isAdmin);
        return new JsonObject<>(0, "ok", pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping("/queryRepairAll2")
    public JsonObject<Repair> queryRepairAll2(Repair repair, HttpServletRequest request,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "15") Integer limit) {
        // 获取当前登录用户
        String authHeader = request.getHeader("Authorization");
        String token = authHeader.substring(7).trim();
        String username = jwtUtil.getUsernameFromToken(token);
        Owner owner = ownerService.queryOwnerByName(username);
        repair.setOwnerId(owner.getId());
        //判断是否是管理员
        boolean isAdmin = isAdminUser(request);
        PageInfo<Repair> pageInfo = repairService.findRepairAll(page, limit, repair, isAdmin);
        return new JsonObject<>(0, "ok", pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping("/queryAll")
    public List<Repairtype> queryAll() {
        return repairtypeService.findList();
    }

    @RequestMapping("/deleteByIds")
    public R deleteByIds(String ids) {
        String[] list = ids.split(",");
        for (String id : list) {
            repairService.delete(Long.parseLong(id));
        }
        return R.ok();
    }

    @ApiOperation(value = "新增")
    @RequestMapping("/add")
    public R add(@RequestBody Repair repair, HttpServletRequest request) {
        // 获取当前登录用户
        String authHeader = request.getHeader("Authorization");
        String token = authHeader.substring(7).trim();
        String username = jwtUtil.getUsernameFromToken(token);
        Owner owner = ownerService.queryOwnerByName(username);

        repair.setOwnerId(owner.getId());
        repair.setStatus(0);
        repair.setComDate(new Date());

        int num = repairService.add(repair);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("新增失败");
    }

    @ApiOperation(value = "更新")
    @RequestMapping("/update")
    public R update(Integer id) {
        Repair repair = new Repair();
        repair.setId(id);
        repair.setStatus(1);
        repair.setHandleDate(new Date());
        int num = repairService.updateData(repair);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("更新失败");

    }

    /**
     * 统计分析
     */
    @RequestMapping("/queryTongJi")
    public List<Tongji> queryTongji() {
        return repairService.queryTongji();
    }
}